package com.gpy.hospital.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.dao.KeshiMapper;
import com.gpy.hospital.cms.pojo.Keshi;
import com.gpy.hospital.cms.service.IKeShiService;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/2/27 19:27
 * @Desc
 **/
@Service
public class CMSKeShiServiceImpl implements IKeShiService {

    @Autowired
    private KeshiMapper keshiMapper;

    @Override
    public PageInfo<Keshi> get(Integer pageNum, Integer pageSize, String name) {

        PageHelper.startPage(pageNum, pageSize);
        List<Keshi> keshis = keshiMapper.selectAll(name);
        return new PageInfo<>(keshis);
    }

    @Override
    public Keshi getById(Integer keshiId) {
        return keshiMapper.selectByPrimaryKey(keshiId);
    }

    @Override
    public ServerResponse save(Keshi keshi) {
        int result = 0;
        if (keshi.getId() == null) {
            result = keshiMapper.insert(keshi);
        } else {
            result = keshiMapper.updateByPrimaryKey(keshi);
        }
        if (result > 0) {
            return ServerResponse.createByErrorMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public ServerResponse delete(Integer keshiId) {
        int result = keshiMapper.deleteByPrimaryKey(keshiId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功！");
        }
        return ServerResponse.createByErrorMessage("删除失败！");
    }

    @Override
    public List<Keshi> getAllByStatus() {
        return keshiMapper.getByStatus();
    }
}
